home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 15 / Example 15.1 / lobby.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-17  |  9.3 KB  |  325 lines

  1. #include "lobby.h"
  2.  
  3. LOBBY::LOBBY()
  4. {
  5.     m_room = 0;
  6.     m_pBackGround = m_pUI = NULL;
  7.     m_pSmallText = m_pCaption = NULL;
  8.     m_pDevice = NULL;
  9.     m_pSprite = NULL;
  10.     m_pMiniText = NULL;
  11.     m_pSmallText = NULL;
  12.     m_pCaption = NULL;
  13. }
  14.  
  15. LOBBY::~LOBBY()
  16. {
  17.     //Release all resources
  18.     if(m_pBackGround)m_pBackGround->Release();
  19.     if(m_pUI)m_pUI->Release();
  20.     if(m_pMiniText)m_pMiniText->Release();
  21.     if(m_pSmallText)m_pSmallText->Release();
  22.     if(m_pCaption)m_pCaption->Release();
  23.     if(m_pSprite)m_pSprite->Release();
  24. }
  25.  
  26. void LOBBY::Init(IDirect3DDevice9* Dev)
  27. {
  28.     m_room = 0;
  29.     m_pDevice = Dev;
  30.  
  31.     //Load textures
  32.     D3DXCreateTextureFromFile(m_pDevice, "textures/background.jpg", &m_pBackGround);
  33.     D3DXCreateTextureFromFile(m_pDevice, "textures/UI.dds", &m_pUI);
  34.  
  35.     //Create fonts
  36.     D3DXCreateFont(m_pDevice, 14, 0, 0, 1, false,  
  37.                    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
  38.                    DEFAULT_PITCH | FF_DONTCARE, "Courier New", &m_pMiniText);    
  39.  
  40.     D3DXCreateFont(m_pDevice, 18, 0, 0, 1, false,  
  41.                    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
  42.                    DEFAULT_PITCH | FF_DONTCARE, "Courier New", &m_pSmallText);    
  43.  
  44.     D3DXCreateFont(m_pDevice, 24, 0, FW_BOLD, 1, false,  
  45.                    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
  46.                    DEFAULT_PITCH | FF_DONTCARE, "Courier New", &m_pCaption);    
  47.  
  48.     //Create sprite and line interface
  49.     D3DXCreateSprite(m_pDevice, &m_pSprite);
  50.     D3DXCreateLine(m_pDevice, &m_pLine);
  51.     m_pLine->SetAntialias(true);
  52.  
  53.     //Init Lobby Content
  54.     RECT nRect[] = {{200, 300, 600, 330}, {30, 540, 640, 570}};
  55.     m_nameTB.Init(nRect[0], 12);
  56.     m_sessionTB.Init(nRect[0], 32);
  57.     m_sendTB.Init(nRect[1], 50);
  58. }
  59.  
  60. void LOBBY::Update(float deltaTime, MOUSE &mouse)
  61. {
  62.     //Update textboxes
  63.     if(m_room == 0)
  64.         m_nameTB.Update(deltaTime, mouse);
  65.     else if(m_room == 2)
  66.         m_sessionTB.Update(deltaTime, mouse);
  67.     else if(m_room == 4)
  68.         m_sendTB.Update(deltaTime, mouse);
  69. }
  70.  
  71. void LOBBY::Draw(MOUSE &mouse)
  72. {
  73.     //Draw background
  74.     D3DXMATRIX sca;
  75.     D3DXMatrixScaling(&sca, 3.125f, 2.35f, 1.0f);
  76.  
  77.     m_pSprite->SetTransform(&sca);
  78.     m_pSprite->Begin(0);
  79.     m_pSprite->Draw(m_pBackGround, NULL, NULL, &D3DXVECTOR3(0.0f, 0.0f, 0.0f), 0xffffffff);
  80.     m_pSprite->End();
  81.     
  82.     D3DXMatrixIdentity(&sca);
  83.     m_pSprite->SetTransform(&sca);
  84.     
  85.     switch(m_room)
  86.     {
  87.         case 0:    //Create Player m_room
  88.         {
  89.             RECT rc = {0, 270, 800, 300};
  90.             m_pCaption->DrawText(NULL, "Enter Your Name:", -1, &rc, DT_CENTER | DT_TOP | DT_NOCLIP, 0xffffffff);
  91.             m_nameTB.active = true;
  92.             DrawTextbox(m_nameTB);
  93.  
  94.             RECT nextR = {520, 360, 600, 390};
  95.             if(m_nameTB.text.size() > 3 && (TextButton("Next", nextR, true, mouse) || KEYDOWN(VK_RETURN)))
  96.             {
  97.                 m_sessionTB.text = m_nameTB.text;
  98.                 m_sessionTB.text += "'s Game";
  99.                 m_room = 1;
  100.             }
  101.             break;
  102.         }
  103.         case 1:    //Host or Join room. (server/client)
  104.         {
  105.             RECT r[] = {{250, 250, 550, 280}, {250, 320, 550, 350}};
  106.             if(TextButton("Host Game", r[0], true, mouse))
  107.             {
  108.                 network.Init(true, (char*)m_nameTB.text.c_str());
  109.                 m_room = 2;
  110.             }
  111.  
  112.             if(TextButton("Join Game", r[1], true, mouse))
  113.             {
  114.                 network.Init(false, (char*)m_nameTB.text.c_str());
  115.                 network.FindSessions();
  116.                 m_room = 3;
  117.             }
  118.  
  119.             break;
  120.         }
  121.         case 2:    //Create Session room
  122.         {
  123.             RECT rc = {0, 270, 800, 300};
  124.             m_pCaption->DrawText(NULL, "Enter Session Name:", -1, &rc, DT_CENTER | DT_TOP | DT_NOCLIP, 0xffffffff);
  125.             m_sessionTB.active = true;
  126.             DrawTextbox(m_sessionTB);
  127.  
  128.             RECT nextR = {520, 360, 600, 390};
  129.             if(m_sessionTB.text.size() > 3 && (TextButton("Next", nextR, true, mouse) || KEYDOWN(VK_RETURN)))
  130.             {
  131.                 network.HostNewSession((char*)m_sessionTB.text.c_str());
  132.                 m_room = 4;
  133.             }
  134.             break;
  135.         }
  136.         case 3:    //Join Session room
  137.         {
  138.             RECT r[] = {{50, 50, 750, 550}, {50, 50, 750, 100}, {600, 60, 740, 90}};
  139.  
  140.             Square(r[0], 0xffffffff);
  141.             Square(r[1], 0xffffffff);
  142.  
  143.             RECT rc = {70, 60, 0, 0};
  144.             m_pCaption->DrawText(NULL, "Active Games", -1, &rc, DT_LEFT | DT_TOP | DT_NOCLIP, 0xffffffff);
  145.             if(TextButton("Refresh", r[2], true, mouse))network.FindSessions();
  146.  
  147.             //Draw list of available sessions
  148.             for(int i=0;i<network.sessions.size();i++)
  149.             {
  150.                 RECT rs = {60, 110 + 40 * i, 600, 140 + 40 * i};
  151.                 char number[10];
  152.                 std::string text = _itoa(i + 1, number, 10);
  153.                 text += ": ";
  154.                 text += network.sessions[i].name;
  155.  
  156.                 if(TextButton((char*)text.c_str(), rs, false, mouse))
  157.                 {
  158.                     HRESULT hr = network.ConnectToSession(i);
  159.  
  160.                     //Connection failed...
  161.                     if(FAILED(hr))
  162.                     {
  163.                         if(hr == DPNERR_HOSTREJECTEDCONNECTION)
  164.                             m_error = "Host rejected connection";
  165.                         else if(hr == DPNERR_NOCONNECTION)
  166.                             m_error = "No connection was made";
  167.                         else if(hr == DPNERR_NOTHOST)
  168.                             m_error = "Host not found";
  169.                         else if(hr == DPNERR_SESSIONFULL)
  170.                             m_error = "Session is full";
  171.                         else if(hr == DPNERR_ALREADYCONNECTED)
  172.                             m_error = "You are already connected";
  173.                         else if(hr == DPNERR_INVALIDAPPLICATION)
  174.                             m_error = "Invalid GUID";
  175.                         else if(hr == DPNERR_INVALIDDEVICEADDRESS)
  176.                             m_error = "Invalid m_pDevice Address";
  177.                         else if(hr == DPNERR_INVALIDFLAGS)
  178.                             m_error = "Invalid Flags";
  179.                         else if(hr == DPNERR_INVALIDHOSTADDRESS)
  180.                             m_error = "Invalid Host Address";
  181.                         else if(hr == DPNERR_INVALIDINSTANCE)
  182.                             m_error = "Invalid GUID 2";
  183.                         else if(hr == DPNERR_INVALIDINTERFACE)
  184.                             m_error = "Invalid Interface";
  185.                         else if(hr == DPNERR_INVALIDPASSWORD)
  186.                             m_error = "Invalid Password";
  187.                         else m_error = "Unknown Error";
  188.  
  189.                         m_room = 999;
  190.                     }
  191.                     else m_room = 4;
  192.                 }
  193.             }
  194.  
  195.             break;
  196.         }
  197.         case 4:    //Chat room
  198.         {
  199.             RECT r[] = {{20, 20, 500, 470}, {20, 20, 500, 70}, {520, 20, 780, 470}, {520, 20, 780, 70}, {20, 490, 780, 580}};
  200.             RECT btn[] = {{660, 540, 770, 570}};
  201.  
  202.             for(int i=0;i<5;i++)
  203.                 Square(r[i], 0xffffffff);
  204.  
  205.             m_sendTB.active = true;
  206.  
  207.             RECT r1[] = {{40, 30, 0, 0}, {540, 30, 0, 0}, {40, 500, 0, 0}};
  208.             m_pCaption->DrawText(NULL, network.activeSession, -1, &r1[0], DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
  209.             m_pCaption->DrawText(NULL, "Players", -1, &r1[1], DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
  210.             m_pCaption->DrawText(NULL, "Send Text:", -1, &r1[2], DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
  211.  
  212.             DrawTextbox(m_sendTB);
  213.  
  214.             //Send text message
  215.             if(TextButton("Send", btn[0], true, mouse) || KEYDOWN(VK_RETURN))
  216.                 if(m_sendTB.text.size() > 0)
  217.                 {
  218.                     std::string c = network.playerName;
  219.                     c += " says: ";
  220.                     c += m_sendTB.text;
  221.                     if(network.server)network.chat.push_back(c);
  222.                     if(network.chat.size() > 20)network.chat.erase(network.chat.begin());
  223.  
  224.                     MSG_TEXT msg((char*)c.c_str());
  225.                     network.Send(&msg);
  226.                     m_sendTB.text.clear();
  227.                 }
  228.  
  229.             //Draw player list
  230.             for(int i=0;i<network.players.size();i++)
  231.             {
  232.                 RECT rc = {530, 90 + 40 * i, 0, 0};
  233.                 m_pCaption->DrawText(NULL, network.players[i].name, -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
  234.             }
  235.             //Draw chat
  236.             for(int i=0;i<network.chat.size();i++)
  237.             {
  238.                 RECT rc = {30, 80 + 19 * (network.chat.size() - i - 1), 0, 0};
  239.                 m_pSmallText->DrawText(NULL, network.chat[i].c_str(), -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
  240.             }
  241.  
  242.             //Check if the connection was terminated
  243.             if(!network.connected)
  244.             {
  245.                 m_error = "Session Terminated";
  246.                 m_room = 999;
  247.             }
  248.  
  249.             break;
  250.         }
  251.         case 999:    //Error room
  252.         {
  253.             RECT rc = {0, 300, 800, 330};
  254.             m_pCaption->DrawText(NULL, m_error.c_str(), -1, &rc, DT_CENTER | DT_TOP | DT_NOCLIP, 0xffffffff);
  255.  
  256.             RECT r = {300, 330, 500, 360}; 
  257.             if(TextButton("OK", r, true, mouse))
  258.             {
  259.                 m_room = 0;
  260.                 m_nameTB.text.clear();
  261.                 m_sessionTB.text.clear();
  262.                 m_sendTB.text.clear();
  263.                 network.Release();
  264.             }
  265.  
  266.             break;
  267.         }
  268.     }    
  269. }
  270.  
  271. void LOBBY::DrawTextbox(TEXTBOX &tb)    //Draws a textbox
  272. {
  273.     Square(tb.rect, 0xffffffff);
  274.  
  275.     RECT rc = {tb.rect.left + 10, tb.rect.top + 6, 0, 0};
  276.     m_pSmallText->DrawText(NULL, tb.GetText().c_str(), -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, 0xffffffff);
  277. }
  278.  
  279. bool LOBBY::TextButton(char text[], RECT r, bool border, MOUSE &mouse)    //A text button...
  280. {
  281.     DWORD col = 0xffffffff;
  282.     if(mouse.Over(r))col = 0xffff0000;
  283.  
  284.     if(border)
  285.     {
  286.         Square(r, col);
  287.         m_pCaption->DrawText(NULL, text, -1, &r, DT_CENTER | DT_VCENTER | DT_NOCLIP, col);
  288.     }
  289.     else 
  290.     {
  291.         RECT rc = {r.left + 10, r.top + 4, 0, 0};
  292.         m_pCaption->DrawText(NULL, text, -1, &rc, DT_LEFT| DT_TOP | DT_NOCLIP, col);
  293.     }
  294.     
  295.     if(mouse.PressInRect(r))
  296.     {
  297.         mouse.DisableInput(300);
  298.         return true;
  299.     }
  300.     else return false;
  301. }
  302.  
  303. void LOBBY::Square(RECT r, DWORD col)        //Draws a simple square
  304. {
  305.     D3DXMATRIX sca;
  306.     D3DXVECTOR2 scale = D3DXVECTOR2(r.right - r.left, r.bottom - r.top);
  307.     D3DXMatrixScaling(&sca, scale.x, scale.y, 1.0f);
  308.     m_pSprite->SetTransform(&sca);
  309.     RECT src = {1,1,2,2};
  310.     m_pSprite->Begin(D3DXSPRITE_ALPHABLEND);
  311.     m_pSprite->Draw(m_pUI, &src, NULL, &D3DXVECTOR3(r.left / scale.x, r.top / scale.y, 0.0f), 0xffffffff);
  312.     m_pSprite->End();
  313.  
  314.     D3DXMatrixIdentity(&sca);
  315.     m_pSprite->SetTransform(&sca);
  316.  
  317.     D3DXVECTOR2 rect[] = {D3DXVECTOR2(r.left, r.top), D3DXVECTOR2(r.right, r.top), 
  318.                           D3DXVECTOR2(r.right, r.bottom), D3DXVECTOR2(r.left, r.bottom), 
  319.                           D3DXVECTOR2(r.left, r.top)};    
  320.  
  321.     m_pLine->SetWidth(2.5f);
  322.     m_pLine->Begin();    
  323.     m_pLine->Draw(rect, 5, col);
  324.     m_pLine->End();
  325. }